<template>
    <el-dialog title="导入excel" :visible="isShowExcel" @close="dialogClose" width="40%">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="excelUpload" label-width="auto" class="demo-ruleForm">
            <el-form-item label="上传文件">
                <el-upload
                class="upload-demo"
            
                
                :before-remove="beforeRemove"
                :limit="1"
                :on-exceed="handleExceed"
                :http-request="upload">
                <!-- :on-preview="handlePreview"
                :on-remove="handleRemove" -->
                
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传excel文件</div>
                </el-upload>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm()" class="pull-right margin-l-25">确定
                </el-button>
                <el-button @click="dialogClose()" class="pull-right">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>
<script>
import { addData,updateExcel } from '@/api/systemAdministrator/administrativeArea';
// import { appendFile } from 'fs';
export default {
    name: 'excel',
    data () {
        return {
            ruleForm: {
                excel: null,
                deleteHistory: false,
            },
            rules: {
                // excel: [
                //     { required: true, message: '请上传文件', trigger: 'blur' }
                // ],
            },
            excel: null,
            // deleteHistory: false,
        };

    },

    props: {
        isShowExcel: {
            type: Boolean
        },
    },
    methods: {
        upload(file){
            return this.handleChange(file,false)
        },
        submitForm () {
            this.$refs.excelUpload.validate((valid) => {
                if (valid) {
                    // updateExcel(this.ruleForm).then((res) => {
                    //     console.log(this.ruleForm);
                    //     if(res){
                    //         console.log(res.msg);
                    //         this.dialogClose();
                    //     }
                    // })
                }
                else {
                    return ;
                }
            });
        },

        dialogClose () {
            this.$emit('dialogClose'); 
        },

        // handleRemove(file) {
        //     console.log(file);
        // },
        // handlePreview(file) {
        //     console.log(file);
        // },
        handleExceed(file) {
            console.log(file);
        },
        beforeRemove(file) {
            return this.$confirm(`确定移除 ${ file.name }？`);
        },
        handleChange(file,deleteHistory)
        {
            console.log(file);
            let formdata =new FormData()
            formdata.append('file',file.file)
            formdata.append('deleteHistory',deleteHistory)
            updateExcel(formdata).then((res) => {
                // console.log(this.ruleForm);
                if(res){
                    console.log(res.msg);
                    this.dialogClose();
                }
            })
        },
        // uploadFile(item) {
        //     console.log(item);
        //     let fileObj = item.file;
        //     const form = new FormData();// FormData 对象
        //     form.append('file', fileObj);// 文件对象  'upload'是后台接收的参数名
        //     $.ajax({
        //         url: "/api/regions/uploadregion",
        //         data: this.ruleForm,
        //         type: "POST",
        //         contentType: "multipart/form-data",
        //         processData: false, //告诉jquery不要对form进行处理
        //         contentType: false, //指定为false才能形成正确的Content-Type
        //         success: function (res) {
                    
        //         }
        //     })
        // }
    },
    mounted () {
        
    }


}
</script>
<style lang="less" scoped>

</style>